#!/usr/bin/python3.7
# -*- coding: utf-8 -*-
# create by 火苗999℃

import pygame 
from maze import *

title = '火苗999℃'
blog = '火苗999℃的博客'
www='www.alom.com.cn'
# pygame
pygame.init()  # 初始化pygame
size = width, height = 1024, 768  # 设置窗口大小
screen = pygame.display.set_mode(size)  # 显示窗口
pygame.display.set_caption(blog)
# 颜色
diamond_color_size = 8
COLOR_RED, COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_BLACK, COLOR_GRAY, COLOR_GOLDEN, COLOR_NO_DIAMOND = list(range(
    diamond_color_size))
COLOR = {
    COLOR_RED: (255, 0, 0),
    COLOR_BLUE: (0, 0, 255),
    COLOR_GREEN: (0, 255, 0),
    COLOR_YELLOW: (255, 255, 0),
    COLOR_BLACK: (0, 0, 0),
    COLOR_GRAY: (250, 240, 230),
    COLOR_GOLDEN : (255,215,0),
    COLOR_NO_DIAMOND: (160, 160, 160),
}
# 格式边长
DIAMOND_LENGTH = 12
# 格子大小
DIAMOND_SIZE = (DIAMOND_LENGTH, DIAMOND_LENGTH)
# 字体
use_font = pygame.font.Font("FONT.TTF", 16)
# 背景
background=pygame.surface.Surface(size).convert()
background.fill(COLOR[COLOR_NO_DIAMOND])
# 楼梯
COLOR_U = (255,128,0)
COLOR_D = (0x64,0x21,0x00)
COLOR_W = (0xd9, 0x46, 0x00)
COLOR_FONT=COLOR[COLOR_BLACK]
# 上
DIAMOND_U=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_U.fill(COLOR_W)
pygame.draw.polygon(DIAMOND_U, COLOR_U, ((0,DIAMOND_LENGTH-1),(DIAMOND_LENGTH//2,0),(DIAMOND_LENGTH-1,DIAMOND_LENGTH-1)))
surface_u = use_font.render("向上楼梯", True, COLOR_FONT)
# 下 
DIAMOND_D=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_D.fill(COLOR_W)
pygame.draw.polygon(DIAMOND_D, COLOR_D, ((0,0),(DIAMOND_LENGTH-1,0),(DIAMOND_LENGTH//2,DIAMOND_LENGTH-1)))
surface_d = use_font.render("向下楼梯", True, COLOR_FONT)
# 双向楼梯
DIAMOND_UD=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_UD.fill(COLOR_W)
pygame.draw.polygon(DIAMOND_UD, COLOR_D, ((0,0),(DIAMOND_LENGTH-1,0),(DIAMOND_LENGTH//2,DIAMOND_LENGTH//2)))
pygame.draw.polygon(DIAMOND_UD, COLOR_U, ((0,DIAMOND_LENGTH-1),(DIAMOND_LENGTH//2,DIAMOND_LENGTH//2+1),(DIAMOND_LENGTH-1,DIAMOND_LENGTH-1)))
surface_ud = use_font.render("上下楼梯", True, COLOR_FONT)
# 通道
DIAMOND_W=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_W.fill(COLOR_W)
# 访问过的墙
DIAMOND_GRAY=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_GRAY.fill(COLOR[COLOR_GRAY])
# 红点
DIAMOND_RED=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_RED.fill(COLOR[COLOR_RED])
# 黄色格子
DIAMOND_YELLOW=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_YELLOW.fill(COLOR[COLOR_YELLOW])
# 未访问的格式
DIAMOND_NO_VISIT=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_NO_VISIT.fill(COLOR[COLOR_BLACK])
surface_nv = use_font.render("未访问的格式", True, COLOR_FONT)
# 未访问的墙
DIAMOND_WALL_NO_VISIT=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_WALL_NO_VISIT.fill(COLOR[COLOR_GOLDEN])
surface_wnv = use_font.render("未访问的墙", True, COLOR_FONT)
# 访问过的格式
DIAMOND_VISIT=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_VISIT.fill(COLOR[COLOR_GREEN])
DIAMOND_VISIT=DIAMOND_W
surface_v = use_font.render("访问过的格式或打通的墙", True, COLOR_FONT)

# 3层使用不同颜色
DIAMOND_O=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_O.fill((255,128,0))
DIAMOND_G=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_G.fill((0,102,51))
DIAMOND_R=pygame.surface.Surface(DIAMOND_SIZE).convert()
DIAMOND_R.fill((102,0,0))

# 时间
clock = pygame.time.Clock()

def draw_maze(maze_map, levels, rows, cols, point, over):
    # 
    screen.blit(background, (0, 0))
    # 格子
    for zi in range(levels):
        for xi in range(cols):
            for yi in range(rows):
                a = zi % 2
                b = zi // 2
                px, py = 1 + xi * DIAMOND_SIZE[0] + a * (cols*DIAMOND_LENGTH+10),  1 + yi * DIAMOND_SIZE[1] + b*(rows*DIAMOND_LENGTH+10)
                if CELL_NO_VISIT == maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_NO_VISIT, (px, py))
                elif WALL_NO_VISIT == maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_WALL_NO_VISIT, (px, py))
                elif STEEL == maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_GRAY, (px, py))
                elif WALL_VISIT == maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_GRAY, (px, py))
                elif NOWALL & maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_W, (px, py))
                elif STAIRS_UD == (STAIRS_UD & maze_map[xi][yi][zi]):
                    screen.blit(DIAMOND_UD, (px, py))
                elif STAIRS_U & maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_U, (px, py)) 
                elif STAIRS_D & maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_D, (px, py))
                elif CELL_VISIT & maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_W, (px, py))
                elif WALL & maze_map[xi][yi][zi]:
                    screen.blit(DIAMOND_GRAY, (px, py))
                else:
                    screen.blit(DIAMOND_YELLOW, (px, py))

    # 当前点
    if not over:
        a = point[2] % 2
        b = point[2] // 2
        px, py = 1 + point[0] * DIAMOND_SIZE[0] + a * (cols*DIAMOND_LENGTH+10),  1 + point[1] * DIAMOND_SIZE[1] + b*(rows*DIAMOND_LENGTH+10)
        screen.blit(DIAMOND_RED, (px, py))
    # 
    posx,posy = 20, ((levels + 1) // 2)*(rows*DIAMOND_LENGTH+10)
    screen.blit(DIAMOND_U, (posx, posy))
    screen.blit(DIAMOND_D, (posx, posy+20))
    screen.blit(DIAMOND_UD, (posx, posy+40))
    screen.blit(DIAMOND_NO_VISIT, (posx, posy+60))
    screen.blit(DIAMOND_WALL_NO_VISIT, (posx, posy+80))
    screen.blit(DIAMOND_VISIT, (posx, posy+100))
    screen.blit(surface_u, (posx+20, posy))
    screen.blit(surface_d, (posx+20, posy+20))
    screen.blit(surface_ud, (posx+20, posy+40))
    screen.blit(surface_nv, (posx+20, posy+60))
    screen.blit(surface_wnv, (posx+20, posy+80))
    screen.blit(surface_v, (posx+20, posy+100))

    if over:
        score_surface = use_font.render("生成完成! by " + title, True, COLOR_FONT, COLOR[COLOR_GRAY])
        screen.blit(score_surface, (posx+20, posy+120))
    return 